#import "UIImage+WBImage.h"
@implementation UIImage (WBImage)
+ (UIImage *)ct_originImageWithName: (NSString *)name {
    return [[UIImage imageNamed:name] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
- (UIImage *)ct_circleImage {
    CGSize var_size = self.size;
    CGFloat var_drawWH = var_size.width < var_size.height ? var_size.width : var_size.height;
    UIGraphicsBeginImageContext(CGSizeMake(var_drawWH, var_drawWH));
    CGContextRef var_context = UIGraphicsGetCurrentContext();
    CGRect var_clipRect = CGRectMake(0, 0, var_drawWH, var_drawWH);
    CGContextAddEllipseInRect(var_context, var_clipRect);
    CGContextClip(var_context);
    CGRect var_drawRect = CGRectMake(0, 0, var_size.width, var_size.height);
    [self drawInRect:var_drawRect];
    UIImage *var_resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return var_resultImage;
}
+ (UIImage *)ct_cutToBigRect:(UIImage *)image size:(CGSize)size {
    UIImage *var_imageRemove = [self ct_imageResize:image size:image.size];
    double var_width = var_imageRemove.size.width * var_imageRemove.scale;
    double var_height = var_imageRemove.size.height * var_imageRemove.scale;
    UIImage *var_img = nil;
    CGImageRef var_imageRef;
    if (var_height / var_width <= size.height / size.width) {
        var_imageRef = CGImageCreateWithImageInRect([var_imageRemove CGImage], CGRectMake((var_width - var_height * (size.width / size.height))/2.0f, 0, var_height * (size.width / size.height), var_height));
    }
    else {
        var_imageRef = CGImageCreateWithImageInRect([var_imageRemove CGImage], CGRectMake(0, (var_height - var_width * (size.height / size.width))/2.0f, var_width, var_width * (size.height / size.width)));
    }
    var_img = [UIImage imageWithCGImage:var_imageRef scale:var_imageRemove.scale orientation:var_imageRemove.imageOrientation];
    CGImageRelease(var_imageRef);
    return var_img;
}
+ (UIImage*)ct_imageResize:(UIImage*)image size:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0,0,size.width, size.height)];
    UIImage *var_newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return var_newImage;
}
+ (UIImage *)ct_compressImage:(UIImage *)image toByte:(NSUInteger)maxLength {
    CGFloat var_compression = 1;
    NSData *var_data = UIImageJPEGRepresentation(image, var_compression);
    if (var_data.length < maxLength) return image;
    CGFloat var_max = 1;
    CGFloat var_min = 0;
    for (int i = 0; i < 6; ++i) {
        var_compression = (var_max + var_min) / 2;
        var_data = UIImageJPEGRepresentation(image, var_compression);
        if (var_data.length < maxLength * 0.9) {
            var_min = var_compression;
        } else if (var_data.length > maxLength) {
            var_max = var_compression;
        } else {
            break;
        }
    }
    UIImage *var_resultImage = [UIImage imageWithData:var_data];
    if (var_data.length < maxLength) return var_resultImage;
    NSUInteger var_lastDataLength = 0;
    while (var_data.length > maxLength && var_data.length != var_lastDataLength) {
        var_lastDataLength = var_data.length;
        CGFloat var_ratio = (CGFloat)maxLength / var_data.length;
        CGSize var_size = CGSizeMake((NSUInteger)(var_resultImage.size.width * sqrtf(var_ratio)),
                                 (NSUInteger)(var_resultImage.size.height * sqrtf(var_ratio))); 
        UIGraphicsBeginImageContext(var_size);
        [var_resultImage drawInRect:CGRectMake(0, 0, var_size.width, var_size.height)];
        var_resultImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        var_data = UIImageJPEGRepresentation(var_resultImage, var_compression);
    }
    return var_resultImage;
}
+ (UIImage *)ct_imageWithColor:(UIColor *)imgColor
{
    CGRect var_imgRect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContextWithOptions(var_imgRect.size, NO, 0);
    [imgColor setFill];
    UIRectFill(var_imgRect);
    UIImage *var_image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return var_image;
}
- (UIImage*)ct_addCenterlogo:(UIImage*)centerLogo logoPosition:(CGRect)logoRect
{
    UIGraphicsBeginImageContext(self.size);
    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
    [centerLogo drawInRect:logoRect];
    UIImage *var_resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return var_resultingImage;
}
- (CGSize)ct_imageSizeByTargetWidth:(CGFloat)targetWidth {
    if (targetWidth <= 0 || CGSizeEqualToSize(CGSizeZero, self.size)) return CGSizeZero;
    CGFloat var_factor = self.size.height / self.size.width;
    return CGSizeMake(targetWidth, targetWidth * var_factor);
}
- (CGSize)ct_imageSizeByTargetHeight:(CGFloat)targetHeight {
    if (targetHeight <= 0 || CGSizeEqualToSize(CGSizeZero, self.size)) return CGSizeZero;
    CGFloat var_fixelW = CGImageGetWidth(self.CGImage);
    CGFloat var_fixelH = CGImageGetHeight(self.CGImage);
    CGFloat var_factor = var_fixelW / var_fixelH;
    return CGSizeMake(targetHeight * var_factor, targetHeight);
}
+ (UIImage *)ct_imageWithCapturedLayer:(CALayer *)layer {
    UIGraphicsBeginImageContextWithOptions(layer.bounds.size, NO, [UIScreen mainScreen].scale);
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *var_screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return var_screenshot;
}
+ (UIImage *)ct_imageWithTextStr:(NSString *)string
                         font:(UIFont *)font
                    textColor:(UIColor *)textColor
                        width:(CGFloat)width
                       height:(CGFloat)height
                textAlignment:(NSTextAlignment)textAlignment
{
    NSDictionary *var_attributeDic = @{NSFontAttributeName:font};
    CGSize var_size = [string boundingRectWithSize:CGSizeMake(width, height)
                                       options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine
                                    attributes:var_attributeDic
                                       context:nil].size;
    NSInteger var_retWidth = ceil(var_size.width);
    NSInteger var_retHeight = ceil(var_size.height);
    if (var_retWidth % 2 != 0) {
        var_retWidth++;
    }
    if (var_retHeight % 2 != 0) {
        var_retHeight++;
    }
    var_size = CGSizeMake(var_retWidth, var_retHeight);
    if ([UIScreen.mainScreen respondsToSelector:@selector(scale)]) {
        if (UIScreen.mainScreen.scale == 2.0) {
            UIGraphicsBeginImageContextWithOptions(var_size, NO, 1.0);
        } else {
            UIGraphicsBeginImageContext(var_size);
        }
    } else {
        UIGraphicsBeginImageContext(var_size);
    }
    CGContextRef var_context = UIGraphicsGetCurrentContext();
    [[UIColor colorWithWhite:0 alpha:0] set];
    CGRect var_rect = CGRectMake(0, 0, var_retWidth, var_retHeight);
    CGContextFillRect(var_context, var_rect);
    NSMutableParagraphStyle *var_paragraph = [[NSMutableParagraphStyle alloc] init];
    var_paragraph.alignment = textAlignment;
    NSDictionary *var_attributes = @{
        NSForegroundColorAttributeName:textColor,
        NSFontAttributeName:font,
        NSParagraphStyleAttributeName:var_paragraph
    };
    [string drawInRect:var_rect withAttributes:var_attributes];
    UIImage *var_image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return var_image;
}
- (UIImage *)ct_croppedImageWithFrame:(CGRect)frame {
    frame = CGRectMake(frame.origin.x * self.scale,
                       frame.origin.y * self.scale,
                       frame.size.width * self.scale,
                       frame.size.height * self.scale);
    CGImageRef var_sourceImageRef = [self CGImage];
    CGImageRef var_newImageRef    = CGImageCreateWithImageInRect(var_sourceImageRef, frame);
    UIImage   *var_newImage       = [UIImage imageWithCGImage:var_newImageRef scale:[self scale] orientation:[self imageOrientation]];
    CGImageRelease(var_newImageRef);
    return var_newImage;
}
+(UIImage *)ct_imageFromText:(NSArray*)arrContent
                 withFont:(CGFloat)fontSize
            withTextColor:(UIColor *)textColor
              withBgImage:(UIImage *)bgImage
              withBgColor:(UIColor *)bgColor
{
    CGFloat var_maxWidth = 64;
    UIFont *var_font = [UIFont fontWithName:@"PingFang SC" size:fontSize];
    NSMutableArray *var_arrHeight = [[NSMutableArray alloc] initWithCapacity:arrContent.count];
    CGFloat var_fHeight = 0.0f;
    for (NSString *var_sContent in arrContent) {
        CGSize var_stringSize = [var_sContent sizeWithFont:var_font constrainedToSize:CGSizeMake(var_maxWidth, 30) lineBreakMode:NSLineBreakByWordWrapping];
        [var_arrHeight addObject:[NSNumber numberWithFloat:var_stringSize.height]];
        var_fHeight += var_stringSize.height;
    }
    CGSize var_newSize = CGSizeMake(var_maxWidth, var_fHeight);
    UIGraphicsBeginImageContextWithOptions(var_newSize,NO,0.0);
    CGContextRef var_ctx = UIGraphicsGetCurrentContext();
    if(bgImage) {
        UIImage* stretchedTopImage = [bgImage stretchableImageWithLeftCapWidth:0 topCapHeight:0];
        [stretchedTopImage drawInRect:CGRectMake(0, 0, var_newSize.width, var_newSize.height)];
    } else {
        if(bgColor) {
            [bgColor set];
            UIRectFill(CGRectMake(0, 0, var_newSize.width, var_newSize.height));
        }
    }
    CGContextSetCharacterSpacing(var_ctx, 10);
    CGContextSetTextDrawingMode (var_ctx, kCGTextFillClip);
    [textColor set];
    int var_nIndex = 0;
    CGFloat var_fPosY = 0.0f;
    for (NSString *var_sContent in arrContent) {
        NSNumber *var_numHeight = [var_arrHeight objectAtIndex:var_nIndex];
        CGRect var_rect = CGRectMake(0, var_fPosY, var_maxWidth , [var_numHeight floatValue]);
        [var_sContent drawInRect:var_rect withFont:var_font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
        var_fPosY += [var_numHeight floatValue];
        var_nIndex++;
    }
    UIImage *var_image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return var_image;
}
@end
